import axios from "axios";
import { urls } from "./url.js";

let getBaseUrl = 'https://custom.koduckya.shop/api'
// let getBaseUrl = "/api";

const request = axios.create({
  baseURL: getBaseUrl,
  timeout: 50000 // 可选，设置超时时间
})

const fetch = (name, data) => {
  let urlList = urls();
  return new Promise((resolve, reject) => {
    let url = urlList[name] || "";
    if (!url) reject("url未添加 请先添加请求地址到url.js");
    url = getBaseUrl + url;
    request
      .post(url, {
        ...data,
      })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        console.log("fetch err", err);
        reject(err || err);
      })
      .finally(() => {
        console.log("fetch finally url:" + url);
      });
  });
};

export default fetch;
